import request from "@/utils/request.js";

const PREFIX = '/user/order'


// 模拟订单数据
export const mockOrders = [
    {
        id: 1,
        orderNo: 'ORD202403150001',
        status: 'pending',
        statusText: '待付款',
        totalAmount: 299,
        items: [
            {
                id: 1,
                name: '玫瑰花束',
                price: 299,
                quantity: 1,
                isReviewed: false,
                image: 'https://pic.nximg.cn/file/20221216/33102792_122033355109_2.jpg'
            }
        ]
    },
    {
        id: 2,
        orderNo: 'ORD202403150002',
        status: 'shipped',
        statusText: '待收货',
        totalAmount: 399,
        items: [
            {
                id: 2,
                name: '百合花束',
                price: 399,
                quantity: 1,
                isReviewed: false,
                image: 'https://pic.nximg.cn/file/20221216/33102792_122033355109_2.jpg'
            }
        ]
    },
    {
        id: 3,
        orderNo: 'ORD202403150003',
        status: 'completed',
        statusText: '已完成',
        totalAmount: 199,
        items: [
            {
                id: 3,
                name: '康乃馨花束',
                price: 199,
                quantity: 1,
                isReviewed: false,
                image: 'https://pic.nximg.cn/file/20221216/33102792_122033355109_2.jpg'
            }
        ]
    }
]

// 获取订单列表
export const getOrders = async (status) => {
    return request.get(`${PREFIX}/list?status=${status ? status : ''}`)
}

// 获取订单商品详情
export const getOrderItem = async (orderId, itemId) => {
    const order = mockOrders.find(o => o.id === Number(orderId))
    const item = order?.items.find(i => i.id === Number(itemId))
    if (!item) throw new Error('订单商品不存在')
    return item
}

// 提交订单评价
export const submitReview = async (data) => {
    await new Promise(resolve => setTimeout(resolve, 500))
    const {orderItemId} = data

    // 更新本地模拟数据
    const order = mockOrders.find(o => o.items.some(i => i.id === orderItemId))
    const item = order?.items.find(i => i.id === orderItemId)
    if (item) {
        item.isReviewed = true
    }

    return true
}

// 再次购买（添加到购物车）
export const buyAgain = async (orderId) => {
    const order = mockOrders.find(o => o.id === orderId)
    if (!order) throw new Error('订单不存在')

    // 返回购物车所需的商品信息
    return order.items.map(item => ({
        id: item.id,
        name: item.name,
        price: item.price,
        quantity: item.quantity,
        image: item.image
    }))
}

// 支付订单
export const payOrder = async (orderId) => {
    return await request.put(`${PREFIX}/${orderId}/pay`)
}

// 取消订单
export const cancelOrder = async (orderId) => {
    return await request.put(`${PREFIX}/${orderId}/cancel`)
}

// 确认收货
export const confirmOrder = async (orderId) => {
    await new Promise(resolve => setTimeout(resolve, 500))
    const order = mockOrders.find(o => o.id === orderId)
    if (!order) throw new Error('订单不存在')

    order.status = 'completed'
    order.statusText = '已完成'
    return true
}

// 获取物流信息
export const getTracking = async (orderNo) => {
    return await request.get(`/user/tracking/${orderNo}`)
    // await new Promise(resolve => setTimeout(resolve, 300))
    // return {
    //     orderNo,
    //     status: '运输中',
    //     traces: [
    //         {
    //             time: '2024-03-15 14:30:00',
    //             content: '快件已签收，签收人：本人'
    //         },
    //         {
    //             time: '2024-03-15 09:30:00',
    //             content: '快件已到达【北京朝阳区某某站点】'
    //         },
    //         {
    //             time: '2024-03-14 20:30:00',
    //             content: '快件已从【北京转运中心】发出'
    //         },
    //         {
    //             time: '2024-03-14 18:30:00',
    //             content: '快件已到达【北京转运中心】'
    //         },
    //         {
    //             time: '2024-03-14 16:30:00',
    //             content: '商家已发货'
    //         }
    //     ]
    // }
}
